package com.wandian.admin.cmd;

import java.io.BufferedWriter;
import java.io.IOException;

import com.wandian.command.AdminCmd;
import com.wandian.command.AdminCommand;
import com.wandian.util.Log;

/**
 * 抽象管理员命令
 * 
 * @author long
 * @Create 2012-10-16 上午11:49:46
 * @QQ 108232706
 * @version 1.0
 * @description AbstractAdminCmd
 */
public abstract class AbstractAdminCmd implements AdminCommand
{
	protected void send(BufferedWriter writer, String str)
	{
		try
		{
			writer.write(str);
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			Log.error("send error", e);
			e.printStackTrace();
		}
	}

	public void execute(String[] params, BufferedWriter writer)
	{
		AdminCmd cmd = getClass().getAnnotation(AdminCmd.class);
		try {
			if (!cmd.paramCounts().contains(String.valueOf(params.length))) {
				send(writer, "参数不正确" + cmd.desc());
				return;
			}

			if (!exec(params, writer))
				send(writer, "参数不正确" + cmd.desc());
		} catch (Exception e) {
			send(writer, cmd.desc());
			Log.error("cmd:" + cmd.code(), e);
		}
	}

	public abstract boolean exec(String[] paramArrayOfString, BufferedWriter paramBufferedWriter);
}